package com.wenlin.myproduction.util;

import android.annotation.SuppressLint;
import android.graphics.Color;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.wenlin.myproduction.bean.InOutRecordBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * @desc: 封装一些代码很多或者常用的方法
 * @author: 文琳
 * @time: 2025/11/12 周三 15:45
 */
public class InOutUtil {
    public static void setHelper(RecyclerView rv, List<InOutRecordBean> list, boolean search, Items items, MultiTypeAdapter adapter) {
        ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                int dragFlags;
                int swipeFlags = 0;
                if (layoutManager instanceof GridLayoutManager) {
                    dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                } else {
                    dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                }
                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                if (search) return true;
                int fromPosition = viewHolder.getAbsoluteAdapterPosition();
                int toPosition = target.getAbsoluteAdapterPosition();
                if (fromPosition < toPosition) {
                    for (int i = fromPosition; i < toPosition; i++) {
                        Collections.swap(list, i, i + 1);
                    }
                } else {
                    for (int i = fromPosition; i > toPosition; i--) {
                        Collections.swap(list, i, i - 1);
                    }
                }
                adapter.notifyItemMoved(fromPosition, toPosition);
                return false;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

            }

            @Override
            public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
                    if (viewHolder != null) viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
                    if (search) {
                        ToastUtils.getInstance().showInfo("请先取消实时搜索");
                        return;
                    }
                }
                super.onSelectedChanged(viewHolder, actionState);
            }

            @Override
            public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);
                viewHolder.itemView.setBackgroundColor(0);
                //保证后面的list是所有的数据，而不是部分数据
                if (search) return;
                if (list != null && !list.isEmpty()) {
                    List<InOutRecordBean> newList = new ArrayList<>();
                    for (int i = 0; i < list.size(); i++) {
                        InOutRecordBean iorb = list.get(i);
                        iorb.setPosition(i);
                        newList.add(iorb);
                    }
                    InOutDataManager.save(newList);
                    if (items != null) {
                        if (!items.isEmpty()) {
                            items.clear();
                            adapter.notifyDataSetChanged();
                        }
                        items.clear();
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }
    public static String[] getDateDescAndMoneyArrayByRegex(String str) {
        String[] arr = new String[3];
        arr[1] = str;
        String regex = "^(\\d{8})?(.*?)?(-?\\d*\\.?\\d*)?$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            arr[0] = matcher.group(1);
            arr[1] = matcher.group(2);
            arr[2] = matcher.group(3);
            //针对几种特殊情况进行处理，包括：直接以“.”结尾(需改成0)、类似“123.”(需去掉小数点)、类似“.132”(需在最前面加上“0”)
            if (Checker.hasWord(arr[2]) && arr[2].contains(".")) {
                arr[2] = arr[2].replace("0+?$", "");//后面的问号表示非贪婪匹配
                arr[2] = arr[2].replace("\\.$", "");//经过上面的去除尾部的0之后，如果直接以“.”结尾则去掉“.”
            }
        }
        //如果金额为空则默认为0
        if (Checker.hasNoWord(arr[2])) arr[2] = "0";
        else if (arr[2].startsWith(".")) arr[2] = "0" + arr[2];
        return arr;
    }

    @SuppressLint("SimpleDateFormat")
    public static String getFormatCreateTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date(System.currentTimeMillis()));
    }
}
